<template>
  <el-dialog title="选择户表" :visible.sync="open" :close-on-click-modal="false" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户编号" prop="terminalID">
        <el-input
          v-model="queryParams.terminalID"
          placeholder="请输入用户编号"
          clearable style="width: 160px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable style="width: 160px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户地址" prop="terminalAddress">
        <el-input
          v-model="queryParams.terminalAddress"
          placeholder="请输入用户地址"
          clearable style="width: 160px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleBatchAddMeter"
          v-hasPermi="['device:terminalsXbInfo:add']"
        >批量挂接户表
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="terminalsXbInfoList" size="mini"
              border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="用户编号" align="center" prop="terminalID"/>
      <el-table-column label="用户名称" align="center" prop="userName"/>
      <el-table-column label="用户地址" align="center" prop="terminalAddress"/>
      <el-table-column label="生产厂家" align="center" prop="supplierID">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.terminal_supplier" :value="scope.row.supplierID"/>
        </template>
      </el-table-column>
      <el-table-column label="口径" align="center" prop="caliber"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </el-dialog>
</template>

<script>
import {getUnbindXbTerminals, batchBindTerminalsXbInfo} from '@/api/leakage/terminalsXbInfo'

export default {
  name: "UnbindMeterSelector",
  dicts: ['terminal_supplier'],
  props: {
    callback: {
      type: Function,
      default: () => {

      }
    }
  },
  data() {
    return {
      editingZoneObj: undefined,
      open: false,
      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 户表档案表格数据
      terminalsXbInfoList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tenantId: null,
        terminalID: null,
        terminalAddress: null,
        caliber: null,
        userName: null,
        meterReading: null,
        easIon: null,
        easIonID: null,
        supplierID: null
      },
    }
  },
  methods: {
    openDialog(zone) {
      this.editingZoneObj = zone
      this.open = true
      this.getList()
    },
    /** 查询户表档案列表 */
    getList() {
      this.loading = true;
      getUnbindXbTerminals(this.queryParams).then(response => {
        this.terminalsXbInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleBatchAddMeter() {
      const params = {
        ids: this.ids.join(','),
        areaId: this.editingZoneObj.areaId
      }

      this.$modal.confirm('是否确认批量挂接选中的户表？').then(function () {
        return batchBindTerminalsXbInfo(params);
      }).then(() => {
        this.callback();
        this.$modal.msgSuccess("批量挂接户表成功");
        this.open = false
      }).catch(() => {
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
}
</script>

<style scoped>

</style>
